package com.crm.dao.before_sale;

import com.crm.pojo.SaleChance;
import com.crm.vo.SaleChanceVO;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface SaleChanceDAO extends CrudRepository<SaleChance,Long>, JpaSpecificationExecutor<SaleChance> {
    //销售机会总数量
    @Query("select count(s) from SaleChance  s where s.saleChanceIsDel='否'")
    public Long getSaleChanceExistCount();
    //分页查询销售机会
    @Query("select new com.crm.vo.SaleChanceVO(salechance,clientinfo.cname,contact.contactName,userinfo.uname)" +
            "from SaleChance salechance,ClientInfo clientinfo,Contact contact,Userinfo userinfo where clientinfo.cid=salechance.clientId and contact.contactid=salechance.contactId and userinfo.uId=salechance.userId and salechance.saleChanceIsDel='否'")
    public List<SaleChanceVO> queryAllSaleChanceByPage(Pageable pageable);

    //根据id删除销售机会
    @Query(nativeQuery = true, value = "update SALE_CHANCE set SALE_CHANCE_IS_DEL='是' where CH_ID=?1")
    @Modifying
    public void deleteSaleChanceById(Long comId);

    /**
     * 根据机会主题查询所有销售机会
     * @param chanceTheme
     * @return
     */
    @Query("select s from SaleChance s where s.saleChanceIsDel='否' and s.chanceTheme = ?1")
    public List<SaleChance> getSaleChanceByTheme(String chanceTheme);



}
